import {userLoginByCode} from '@/service/user'
export function getUrlValue(url,kValue) {
  var reg = /([^?&=]+)=([^?&=]+)/g, obj = {}, str = url;
  url.replace(reg, function () {
      obj[arguments[1]] = arguments[2];
  })
  console.log(obj);
  for (var keyName in obj) {
      if (keyName == kValue) {
          return obj[keyName];
          // break;
      }
  }
}

export function filterHours(seconds){
    if(seconds === 0){
        return 0;
    }
    const hours = (seconds / 3600).toFixed(0);
    return Number(hours);
}

export function login(isSet = true) {
    return new Promise(async (resolve, reject) => {
      // 1. 获得微信 code
      console.log(122);
      const codeResult = await uni.login();
          console.log(codeResult,'codeResult');
      if (codeResult.errMsg !== 'login:ok') {
        return resolve(false);
      }
      // 2. 社交登录
      userLoginByCode({type:34, code:codeResult.code, state:'default'}).then(loginResult=>{
        if(isSet){
          uni.setStorageSync('openid', loginResult.openid);
          uni.setStorageSync('accessToken',loginResult.accessToken)
          console.log(loginResult);
          uni.$emit('login');
          console.log('注册全局登陆⌚️');
        }
        return resolve(true);
      }).catch(()=>{
        return reject(false);
      });  
        
    });
  };